package com.Yourweibo;

import java.util.List;

import weibo4android.Count;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Yourweibo.status.StatusInformation;
import com.Yourweibo.status.TimeLineConstant;
import com.Yourweibo.util.DataProcessing;

public class StatusDetailActivity extends Activity {
	private ViewHolder viewHolder;
	private Weibo weibo;
	private List<Count> countlist=null;
	static class ViewHolder{
		ImageView UserPhoto;
		TextView UserName;
		TextView StatusText;
		TextView StatusInfor;
		TextView StatusTime;
		ImageView StatusImage;
		RelativeLayout StatusImageFreamwork;
		Button StatusRetweeted;
		Button StatusComment;
		
		
		LinearLayout RetweedFreamwork;
	 
		TextView RetweedStatusText;
		TextView RetweedStatusInfor;
		TextView RetweedStatusTime;
		ImageView RetweedStatusImage;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.status_detail_layout);
		weibo=TimeLineConstant.getInstance().getWeibo();
		viewHolder=new ViewHolder();
		//状态内容
	 	viewHolder.StatusText=(TextView)this.findViewById(R.id.status_detail_status_text);
	 	//取得状态时间
	 	viewHolder.StatusTime=(TextView)this.findViewById(R.id.status_detail_status_time);
	//	viewHolder.userName.setText(user.getName());
	 	viewHolder.StatusImageFreamwork=(RelativeLayout)this.findViewById(R.id.status_detail_simage_framework);
	 	//取得用户头像
	  	viewHolder.UserPhoto=(ImageView)this.findViewById(R.id.status_detail_userphoto_image);
	  	//用户名
	  	viewHolder.UserName=(TextView)this.findViewById(R.id.status_detail_username_text);
	  	//取得状态图片
		viewHolder.StatusImage=(ImageView)this.findViewById(R.id.status_detail_status_image);
		//取得状态信息
		viewHolder.StatusInfor=(TextView)this.findViewById(R.id.status_detail_retweeted_status_info);
		//转发微博视图
		viewHolder.RetweedFreamwork=(LinearLayout)this.findViewById(R.id.status_detail_retweeted_freamwork);
		//被转发用户 状态内容
		viewHolder.RetweedStatusText=(TextView)this.findViewById(R.id.status_detail_retweeted_status_text);
		//被转发用户 取得状态时间
	 	viewHolder.RetweedStatusTime=(TextView)this.findViewById(R.id.status_detail_retweeted_status_time);
	 	//被转发用户 取得状态时间
	 	viewHolder.RetweedStatusImage=(ImageView)this.findViewById(R.id.status_detail_retweeted_status_image);
	 	//转发
	 	viewHolder.StatusRetweeted=(Button)this.findViewById(R.id.status_detail_retweed_button);
	 	//评论
	 	viewHolder.StatusComment=(Button)this.findViewById(R.id.status_detail_comment_button);
	 	putStatusData();
	}
	public void putStatusData(){
		Bundle bundle=getIntent().getExtras();
		Status status=(Status)bundle.getSerializable("CurrentStatus");
		final String photoUrl=status.getUser().getProfileImageURL().toString();
		viewHolder.UserPhoto.setTag(photoUrl);
		DataProcessing.loadHttpImage(photoUrl, viewHolder.UserPhoto,this);
    	
    	final String photoUrl2=status.getThumbnail_pic();
		if(photoUrl2!=null&&!photoUrl2.equals("")){
			//异步加载状态图片
		
			DataProcessing.loadHttpImage(photoUrl2,viewHolder.StatusImage,this.getApplicationContext());
			viewHolder.StatusImageFreamwork.setVisibility(View.VISIBLE);
			
		}else{
			viewHolder.StatusImageFreamwork.setVisibility(View.GONE);	
			
		}
		viewHolder.StatusImage.setTag(status.getBmiddle_pic());
		viewHolder.StatusImage.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DataProcessing.LoadlargeImage(v,(String)v.getTag());
			}});
		//加载状态 text 时间
		LoadCountData(status.getId()+"");
		//设置高亮
		viewHolder.UserName.setText(status.getUser().getName());
		 
		viewHolder.StatusText.setText(DataProcessing.statusSetHighligth(status));
		viewHolder.StatusText.setMovementMethod(LinkMovementMethod.getInstance());
	 	viewHolder.StatusTime.setText(status.getCreatedAt().toLocaleString());
		//加载状态 信息  转发 收藏 评论
	 	//count
	 	List<Count> countlist;
		try {
			countlist = weibo.getCounts(status.getMid());
		 	viewHolder.StatusRetweeted.setText("转发("+countlist.get(0).getRt()+")");	 	
		 	viewHolder.StatusComment.setText("评论("+countlist.get(0).getComments()+")");	
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	 	
		//是否有转发
	 
		Status reTweetedStatus=	status.getRetweeted_status();
			
		if(reTweetedStatus!=null){
			viewHolder.RetweedFreamwork.setVisibility(View.VISIBLE);
			viewHolder.RetweedStatusText.setText(DataProcessing.statusSetHighligth(reTweetedStatus));
			viewHolder.RetweedStatusText.setMovementMethod(LinkMovementMethod.getInstance());
			viewHolder.RetweedStatusTime.setText(status.getCreatedAt().toLocaleString());
	    	final String photoUrl3=reTweetedStatus.getThumbnail_pic();
	    	if(photoUrl3!=null&&!photoUrl3.equals("")){
	    		DataProcessing.loadHttpImage(photoUrl3,viewHolder.RetweedStatusImage,this);
	      	}		
			
	 
		}else{
			viewHolder.RetweedFreamwork.setVisibility(View.GONE);
			
		}
		
	}
	private Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.arg1){
			case 1://加载状态评论和转发
				viewHolder.StatusRetweeted.setText("转发("+countlist.get(0).getRt()+")");	 	
			 	viewHolder.StatusComment.setText("评论("+countlist.get(0).getComments()+")");	
				break;
			case 2://加载转发状态评论和转发
				viewHolder.StatusRetweeted.setText("转发("+countlist.get(0).getRt()+")");	 	
			 	viewHolder.StatusComment.setText("评论("+countlist.get(0).getComments()+")");	
				
				break;
			case 3://加载评论
			break;
			
			}
 
					}
		
		
	};
    public void LoadCountData(final String mid){
    	new Thread(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				 
				try {
					countlist = weibo.getCounts(mid);
				 
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Message msg=handler.obtainMessage();
				msg.arg1=1;
				handler.sendMessage(msg);
			}
    
    	}.start();
    	
    	
    }
}
